package com.getword.web.controller;


import com.getword.domain.Admin;
import com.getword.domain.User;
import com.getword.service.AdminService;
import com.getword.web.model.Result;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/api/admin")
public class ApiAdminController {
    @Autowired
    private AdminService service;

    @PostMapping
    public Result create(User user, String token){
        return service.addAdmin(user, token);
    }

    @PostMapping("/login")
    public Result login(User user, HttpServletRequest request){
        return service.login(user, request);
    }

    // 获取登陆的管理员信息
    @GetMapping("/getLoginInfo")
    public Result getLoginInfo(HttpServletRequest request, String token){
        Object adminInfo = request.getSession().getAttribute(token);
        if(StringUtils.isBlank(token)||adminInfo==null){
            return Result.fail("未登录");
        }
        return Result.success(adminInfo);
    }
}
